package com.mooc.lib_video.ui.detail

import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.databinding.ObservableField
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.mooc.lib_common.bus.LiveDataBus
import com.mooc.lib_network.entity.NetResult
import com.mooc.lib_video.model.CommentsEntity
import com.mooc.lib_video.model.DetailFeedCommentEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class DetailViewModel : ViewModel(){

    val repo = DetailRepo()
    val commentsBus = LiveDataBus<DetailFeedCommentEntity>()
    var commentText: ObservableField<String> = ObservableField()
    val faceFlag = ObservableField<Int>(View.GONE)
    var itemId = 0L
    var userId = 0L

    fun initData(id : Int,itemId : Long,userId : Long){
        viewModelScope.launch(Dispatchers.IO){
            val result = repo.comments(id,itemId,50,userId)
            if (result is NetResult.Success){
                commentsBus.postValue(result.data)
            }
        }
    }

    //发布评论
    //commentText: String,
    //height: Int,
    //image_url: String,
    //itemId: Long,
    //userId: Long,
    //video_url: String,
    //width: Int
    fun sendComment(){
        if (TextUtils.isEmpty(commentText.get())){
            ToastUtils.showShort("请输入评论内容")
            return
        }
        viewModelScope.launch(Dispatchers.IO){
            val result = repo.sendComment(commentText.get()!!,0,"",itemId,userId,"",0)
            if (result is NetResult.Success){
                var data : ArrayList<CommentsEntity>?= ArrayList()
                data!!.add(result.data.data!!)
                var entity = DetailFeedCommentEntity()
                entity.data = data
                commentText.set("")
                faceFlag.set(View.GONE)
                commentsBus.postValue(entity)
            }
        }
    }

    //显示隐藏表情处理逻辑
    fun replaceEmoji(){
        if (faceFlag.get() == View.GONE){
            faceFlag.set(View.VISIBLE)
        }else{
            faceFlag.set(View.GONE)
        }
    }

}